;; puzzle_15
;; license: http://code.google.com/p/jc-pheap/source/browse/trunk/jc-pheap/license.txt

(ns clj-algorithms.a-star
   (:import persistent_heap.PersistentHeap))

(defn a-star [user-state goal-state score-fn trans-fn print-fn]
    (loop [heap (new PersistentHeap)
           current-state user-state
           current-score (score-fn user-state goal-state)]
      (do
        ;(println current-score)
        (if (= 0 current-score)
          (print-fn current-state)
          (let [loaded-heap (reduce #(.insert %1 %2 (score-fn %2 goal-state))
                            heap
                            (trans-fn current-state))
                min-data (.minData loaded-heap)
                min-pri  (.minPri loaded-heap)]
             (recur (.deleteMin loaded-heap) min-data min-pri))))))